﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoreManagementSystem.DataAccessLayer;
using StoreManagementSystem.DomainObjects;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace StoreManagementSystem.Register
{
    public partial class Default : System.Web.UI.Page
    {
        DataAccessLayer.SQLDAOFactory doaFactory;

        protected void Page_Load(object sender, EventArgs e)
        {
            doaFactory = new DataAccessLayer.SQLDAOFactory(new DataAccessLayer.SQLDataProvider("MySQLConnectionString"));
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            RegisterUser();
        }

        private void RegisterUser()
        {
            if (validateFields())
            {
                IDataAccessObject<DomainObjects.Entity> userDAO = doaFactory.createDAO("storeuser");

                if (userDAO.get(tbFirstName.Text, tbLastName.Text, tbUserName.Text) != null)
                    displayMessage("Sorry, but this name has already been taken.");
                {
                    StoreUser user = new StoreUser(System.Guid.NewGuid().ToString(), tbFirstName.Text, tbLastName.Text, tbPassword.Text, tbUserName.Text, tbEmail.Text);
                    userDAO.create(user);

                    if (userDAO.get(tbFirstName.Text, tbLastName.Text, tbUserName.Text, tbEmail.Text) == null)
                        displayMessage("User not created successfully.");
                    else
                        Response.Redirect("~/Register/Finished/");
                }
            }
        }

        private bool validateFields()
        {
            if (tbFirstName.Text == "")
            {
                displayMessage("First name is required.");
                return false;
            }

            if (tbLastName.Text == "")
            {
                displayMessage("Last name is required.");
                return false;
            }

            if (tbEmail.Text == "")
            {
                displayMessage("Email is required.");
                return false;
            }

            if (tbEmailConfirm.Text == "")
            {
                displayMessage("Email confirmation is required.");
                return false;
            }

            if (tbUserName.Text == "")
            {
                displayMessage("Username is required.");
                return false;
            }

            if (tbPassword.Text == "")
            {
                displayMessage("Password is required.");
                return false;
            }

            // check that the password is at least 8 characters
            //if (tbPassword.Text.Length < 8)
            //{
            //    displayMessage("Password must be at least 8 characters.");
            //    return false;
            //}

            if (tbPassword.Text.Length > 15)
            {
                displayMessage("Password must be less than 15 characters.");
                return false;
            }

            if (tbPasswordConfirm.Text == "")
            {
                displayMessage("Password confirmation is required.");
                return false;
            }

            if(!validEmail(tbEmail.Text))
            {
                displayMessage("Email is not valid.");
                return false;
            }

            if (!tbEmail.Text.Equals(tbEmailConfirm.Text))
            {
                displayMessage("Emails do not match.");
                return false;
            }

            if (!tbPassword.Text.Equals(tbPasswordConfirm.Text))
            {
                displayMessage("Passwords do not match.");
                return false;
            }

            if (tbPassword.Text.Equals(tbUserName.Text))
            {
                displayMessage("Password cannot be the same as your username.");
                return false;
            }

            //if (!Page.IsValid)
            //{
            //    displayMessage("Incorrect captcha. Please try again.");
            //    return false;
            //}
            
            return true;
        }

        private bool validEmail(string email)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(email, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
        }

        private void displayMessage(string message)
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = message;
        }

        
    }
}